package com.ruihu.admin.module.business.tasknode.controller;

import com.ruihu.admin.common.anno.NoValidPrivilege;
import com.ruihu.admin.common.anno.OperateLog;
import com.ruihu.admin.common.domain.PageResultDTO;
import com.ruihu.admin.common.controller.BaseController;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.common.domain.ValidateList;
import com.ruihu.admin.module.business.tasknode.domain.dto.DefectTypeAddDTO;
import com.ruihu.admin.module.business.tasknode.domain.dto.DefectTypeDeleteDTO;
import com.ruihu.admin.module.business.tasknode.domain.dto.DefectTypeUpdateDTO;
import com.ruihu.admin.module.business.tasknode.domain.dto.DefectTypeQueryDTO;
import com.ruihu.admin.module.business.tasknode.domain.vo.DefectTypeVO;
import com.ruihu.admin.module.business.tasknode.domain.vo.DefectTypeExcelVO;
import com.ruihu.admin.module.business.tasknode.service.DefectTypeService;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * [ 缺陷类型配置 ]
 *
 * @author Tony
 * @version 1.0
 * @company 瑞虎（南京）信息技术有限公司
 * @copyright (c)  瑞虎（南京）信息技术有限公司Inc. All rights reserved.
 * @date 2022-09-04 10:46:11
 * @since JDK1.8
 */
@RestController
@Api(tags = {"缺陷类型配置"})
@OperateLog
public class DefectTypeController extends BaseController {

    @Autowired
    private DefectTypeService defectTypeService;

    @ApiOperation(value = "分页查询缺陷类型配置",notes = "@author Tony")
    @PostMapping("/defectType/page/query")
    public ResponseDTO<PageResultDTO<DefectTypeVO>> queryByPage(@RequestBody DefectTypeQueryDTO queryDTO) {
        return defectTypeService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "查询缺陷类型",notes = "@author Tony")
    @GetMapping("/defectType/query")
    @NoValidPrivilege
    public ResponseDTO<List<DefectTypeVO>> queryDefectType() {
        return ResponseDTO.succData(defectTypeService.queryDefectType());
    }

    @ApiOperation(value = "添加缺陷类型配置",notes = "@author Tony")
    @PostMapping("/defectType/add")
    public ResponseDTO<String> add(@RequestBody @Validated DefectTypeAddDTO addTO){
        return defectTypeService.add(addTO);
    }

    @ApiOperation(value="修改缺陷类型配置",notes = "@author Tony")
    @PostMapping("/defectType/update")
    public ResponseDTO<String> update(@RequestBody @Validated DefectTypeUpdateDTO updateDTO){
        return defectTypeService.update(updateDTO);
    }

    @ApiOperation(value="删除缺陷类型配置",notes = "@author Tony")
    @PostMapping("/defectType/delete")
    public ResponseDTO<String> delete(@RequestBody @Validated DefectTypeDeleteDTO deleteDTO) {
        return defectTypeService.delete(deleteDTO);
    }

    @ApiOperation(value="批量删除缺陷类型配置",notes = "@author Tony")
    @PostMapping("/defectType/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return defectTypeService.deleteByIds(idList);
    }

    @ApiOperation(value = "批量导出", notes = "@author Tony")
    @PostMapping("/defectType/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //查询数据
        List<DefectTypeExcelVO> defectTypeList = defectTypeService.queryBatchExportData(idList);
        //导出操作
        ExportParams ex = new ExportParams("缺陷类型配置", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, DefectTypeExcelVO.class, defectTypeList);
        downloadExcel("缺陷类型配置", workbook, response);
    }

    @ApiOperation(value = "导出全部", notes = "@author Tony")
    @PostMapping("/defectType/export/all")
    public void exportAll(@RequestBody @Validated DefectTypeQueryDTO queryDTO, HttpServletResponse response) {
        //查询数据
        List<DefectTypeExcelVO> defectTypeList = defectTypeService.queryAllExportData(queryDTO);
        //导出操作
        ExportParams ex = new ExportParams("缺陷类型配置", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, DefectTypeExcelVO.class, defectTypeList);
        downloadExcel("缺陷类型配置", workbook, response);
    }

}
